package com.ui;

import Client.Client;
import Good.Good;

import javax.swing.*;
import java.awt.event.*;

public class type_1 extends JDialog {
    public static Client c;
    private JPanel contentPane;
    private JButton button_next;
    private JButton buttonCancel;
    private JComboBox comboBox1;
    private JButton Button_go;
    private JTextField textField1;
    private JButton Button_ser;
    private JTextField textField2;
    private JSpinner spinner1;
    private JButton Button_pur;
    private JButton Button_add;
    private JButton Button_in;
    private JButton Button_pur1;
    private JButton Button_add1;
    private JButton Button_in1;
    private JButton Button_pur2;
    private JButton Button_add2;
    private JButton Button_in2;
    private JButton Button_pur3;
    private JButton Button_add3;
    private JButton Button_in3;
    private JButton Button_pur4;
    private JButton Button_add4;
    private JButton Button_in4;
    private JButton Button_pur5;
    private JButton Button_add5;
    private JButton Button_in5;
    private JSpinner spinner2;
    private JSpinner spinner3;
    private JSpinner spinner4;
    private JSpinner spinner5;
    private JSpinner spinner6;
    private JButton button_pre;
    private JButton Button_usr;
    private JTextField textField3;
    private JLabel label1;
    private JLabel label11;
    private JButton button_update;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label66;
    private JLabel label55;
    private JLabel label44;
    private JLabel label33;
    private JLabel label22;
    private JTextArea textArea1;

    public JTextArea getTextArea(String str) {
//        textArea1.append("开始认证\n");
        textArea1.append(str);
        //textArea.append("sadsad55\n");
        return textArea1;
    }

    public type_1() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(button_next);

        button_next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // 点击 X 时调用 onCancel()
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 遇到 ESCAPE 时调用 onCancel()
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        button_update.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onINTIAL();
                JTextArea textArea1=getTextArea("发送给V的报文:\n"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:\n"+DENGLU.c.p+"\n");
            }
        });
        Button_usr.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ONusr();
            }
        });
        Button_pur.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(5).setNum((Integer) spinner1.getValue());
                buy(5);
                JTextArea textArea1=getTextArea("发送给V的报文:"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:"+DENGLU.c.p+"\n");
            }
        });
        Button_pur1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(4).setNum((Integer) spinner2.getValue());
                buy(4);
                JTextArea textArea1=getTextArea("发送给V的报文:"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:"+DENGLU.c.p+"\n");
            }
        });
        Button_pur2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(3).setNum((Integer) spinner3.getValue());
                buy(3);
                JTextArea textArea1=getTextArea("发送给V的报文:"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:"+DENGLU.c.p+"\n");
            }
        });
        Button_pur3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(2).setNum((Integer) spinner4.getValue());
                buy(2);
                JTextArea textArea1=getTextArea("发送给V的报文:"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:"+DENGLU.c.p+"\n");
            }
        });
        Button_pur4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(1).setNum((Integer) spinner5.getValue());
                buy(1);
                JTextArea textArea1=getTextArea("发送给V的报文:"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:"+DENGLU.c.p+"\n");
            }
        });
        Button_pur5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(0).setNum((Integer) spinner6.getValue());
                buy(0);
                JTextArea textArea1=getTextArea("发送给V的报文:"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:"+DENGLU.c.p+"\n");
            }
        });


        Button_add5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(0).setNum((Integer) spinner6.getValue());
                addS(0);
            }
        });
        Button_in5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.openORL(DENGLU.c.goods.get(0).getURL());
            }
        });
        Button_ser.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JTextArea textArea1=getTextArea("发送给V的报文:"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:"+DENGLU.c.p+"\n");
                String text=textField1.getText();
                DENGLU.c.goods.clear();
                DENGLU.c.process(DENGLU.c.GetByName(text));
                label1.setText(DENGLU.c.goods.get(0).getName());
                label11.setText(String.valueOf(DENGLU.c.goods.get(0).getprice()));

            }
        });
        Button_go.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               String str1= (String) comboBox1.getSelectedItem();
                DENGLU.c.goods.clear();
                for (int i=0;i<6;i++){
                    DENGLU.c.process(DENGLU.c.GetByType(str1));
                }
                try {
                    label1.setText(DENGLU.c.goods.get(0).getName());
                    label11.setText(String.valueOf(DENGLU.c.goods.get(0).getprice()));
                }
                catch (Exception ee)
                {
                    return;
                }
                try {
                    label2.setText(DENGLU.c.goods.get(1).getName());
                    label22.setText(String.valueOf(DENGLU.c.goods.get(1).getprice()));
                }
                catch (Exception ee)
                {
                    return;
                }
                try
                {
                    label3.setText(DENGLU.c.goods.get(2).getName());
                    label33.setText(String.valueOf(DENGLU.c.goods.get(2).getprice()));
                }
                catch (Exception ee)
                {
                    return;
                }
                try {
                    label4.setText(DENGLU.c.goods.get(3).getName());
                    label44.setText(String.valueOf(DENGLU.c.goods.get(3).getprice()));
                }
                catch (Exception ee)
                {
                    return;
                }
                try {
                    label5.setText(DENGLU.c.goods.get(4).getName());
                    label55.setText(String.valueOf(DENGLU.c.goods.get(4).getprice()));
                }
                catch (Exception ee)
                {
                    return;
                }
                try {
                    label6.setText(DENGLU.c.goods.get(5).getName());
                    label66.setText(String.valueOf(DENGLU.c.goods.get(5).getprice()));
                }
                catch (Exception ee)
                {
                    System.out.println(ee);
                }

                JTextArea textArea1=getTextArea("发送给V的报文:"+DENGLU.c.pk+"\n");
                textArea1=getTextArea("数字签名:\n"+DENGLU.c.key_dv+"\n");
                textArea1=getTextArea("解密后的明文:"+DENGLU.c.p+"\n");


            }
        });
        Button_add4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(1).setNum((Integer) spinner5.getValue());
                addS(1);
            }
        });
        Button_add3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(2).setNum((Integer) spinner4.getValue());
                addS(2);
            }
        });
        Button_add2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(3).setNum((Integer) spinner3.getValue());
                addS(3);
            }
        });
        Button_add1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(4).setNum((Integer) spinner2.getValue());
                addS(4);
            }
        });
        Button_add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DENGLU.c.goods.get(5).setNum((Integer) spinner1.getValue());
                addS(5);
            }
        });
        Button_in4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Client.openORL(DENGLU.c.goods.get(1).getURL());
            }
        });
        Button_in3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Client.openORL(DENGLU.c.goods.get(2).getURL());
            }
        });
        Button_in2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Client.openORL(DENGLU.c.goods.get(3).getURL());
            }
        });
        Button_in1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Client.openORL(DENGLU.c.goods.get(4).getURL());
            }
        });
        Button_in.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Client.openORL(DENGLU.c.goods.get(5).getURL());
            }
        });
    }
    private void buy(int i){
        DENGLU.c.Buy(DENGLU.c.goods.get(i));
    }

    private void addS(int i)
    {
        DENGLU.c.shopping.add(DENGLU.c.goods.get(i));
    }
    private void onINTIAL(){
        DENGLU.c.goods.clear();
        for (int i=0;i<6;i++){
            DENGLU.c.process(c.Check());
        }
        try {
            label1.setText(DENGLU.c.goods.get(0).getName());
            label11.setText(String.valueOf(DENGLU.c.goods.get(0).getprice()));
        }
        catch (Exception e)
        {
            return;
        }
        try {
            label2.setText(DENGLU.c.goods.get(1).getName());
            label22.setText(String.valueOf(DENGLU.c.goods.get(1).getprice()));
        }
        catch (Exception e)
        {
            return;
        }
        try
        {
            label3.setText(DENGLU.c.goods.get(2).getName());
            label33.setText(String.valueOf(DENGLU.c.goods.get(2).getprice()));
        }
        catch (Exception e)
        {
            return;
        }
        try {
            label4.setText(DENGLU.c.goods.get(3).getName());
            label44.setText(String.valueOf(DENGLU.c.goods.get(3).getprice()));
        }
        catch (Exception e)
        {
            return;
        }
        try {
            label5.setText(DENGLU.c.goods.get(4).getName());
            label55.setText(String.valueOf(DENGLU.c.goods.get(4).getprice()));
        }
        catch (Exception e)
        {
            return;
        }
        try {
            label6.setText(DENGLU.c.goods.get(5).getName());
            label66.setText(String.valueOf(DENGLU.c.goods.get(5).getprice()));
        }
        catch (Exception e)
        {
            System.out.println(e);
        }

    }

    private void ONusr(){
        new Usr_ui().main();
    }

    private void onOK() {
        // 在此处添加您的代码
        JTextArea textArea1=getTextArea("send to V:"+DENGLU.c.pk+"\n");
        textArea1=getTextArea("decrypt the msg:"+DENGLU.c.p+"\n");
        dispose();
    }

    private void onCancel() {
        // 必要时在此处添加您的代码
        dispose();
    }

    public void main() {
        type_1 dialog = new type_1();
        dialog.pack();
        dialog.setVisible(true);
        //System.exit(0);
    }
}
